package com.niren.relay.service.redisservice;

import com.niren.relay.common.constant.constant;
import com.niren.relay.common.convert.convert;
import com.niren.relay.common.devinfo.AddDevInfo;
import com.niren.relay.entity.SysDeviceManage;
import com.niren.relay.redis.RedisString;
import com.niren.relay.redis.formatjson.String2Json;
import org.springframework.boot.autoconfigure.cache.CacheProperties;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class SysDeviceManageService {
    SysDeviceManage sdm = new SysDeviceManage();

    /**
     * 创建设备
     * @param dev
     * @param ch
     * @param chType
     * */
    private SysDeviceManage CreateDevice(AddDevInfo dev,int ch,int chType ){

        SysDeviceManage sdm = new SysDeviceManage();

        sdm.setDevMac(dev.getMac());
        sdm.setDevName(dev.getDevName());
        if(chType == 0)
            sdm.setNodeName(dev.getDevName() + "out" + ch);
        else
            sdm.setNodeName(dev.getDevName() + "in" + ch);
        sdm.setNodeCh(ch);
        sdm.setNodeType(chType);
        sdm.setRtValue(0);
        sdm.setIsOnline(0);

        return sdm;
    }

    /**
     * 添加mac地址到String中，并写入到redis
     * @param dev
     * @return 返回所有mac字符串，以  |  符号分隔
     * */
    public String addMac2String(AddDevInfo dev){

        RedisString rString = new RedisString(constant.hostOrIp);
        String macsStr = rString.getValue(constant.macsKey);
        if(macsStr == null || macsStr == "") macsStr = "";
        if(macsStr != "") macsStr += "|";
        macsStr += dev.getMac();
        rString.setValue(constant.macsKey,macsStr);

        return macsStr;
    }

    /**
     * 根据添加信息，创建设备节点信息
     * @param dev
     * @return 创建完成返回true,有数据为空，返回false
     * */
    public  boolean CreateDeviceByDevInfo(AddDevInfo dev){
        if(dev.getOutCh() == null || dev.getInCh() == null || dev.getMac() == null){
            return false;
        }
        if(dev.getDevName() == null) return false;

        String macsStr = addMac2String(dev);//添加mac

        SysDeviceManage sdm = null;
        String2Json<SysDeviceManage> sj = new String2Json<SysDeviceManage>();
        for(int index=0;index < dev.getOutCh();index++){
            sdm = CreateDevice(dev,index+1,0);
            sj.Class2Json(convert.getDevKey(sdm),sdm);
        }
        for(int index=0;index<dev.getInCh();index++){
            sdm = CreateDevice(dev,index+1,1);
            sj.Class2Json(convert.getDevKey(sdm),sdm);
        }
        return true;
    }

    public HashMap<String,Object> getAllDevInfo(){
        HashMap<String,Object> modelMap = new HashMap<String,Object>();
        String2Json<SysDeviceManage> sj = new String2Json<SysDeviceManage>();
        HashSet<String> keys = sj.getKeys("*");
        for(String key : keys){
            if(key.equals(constant.macsKey)) continue;
            SysDeviceManage sdm = sj.Json2Class(key,SysDeviceManage.class);
            modelMap.put(key,sdm);
        }
        return modelMap;
    }
}
